<?php
namespace app\common\model;

class MemberCoinRecord extends Base
{
	protected $readonly = ['id'];
	protected $auto=[];
	protected $insert=['create_time'];

	static $operate = [
				'transfer' => '会员转账',
				'convert' => '货币转换',
				'charge' => '充值',
				'withdraw' => '提现',
                'change' => '管理员增减',
                'active' => '激活会员',
                'buy' => '购物扣费',
                'rejected' => '购物驳回退费',
                'upgrade' => '原点升级',

                'lingshou' => '零售利润',
                'xiaoshou' => '销售补贴',
                'fuwu' => '服务补贴',
                'world' => '全体分红',
                'jiaquan' => '加权分红',

                'withdraw_refuse' => '拒绝提现',

			];
	static $flag = [
				'inc' => '增加',
				'dec' => '减少',
			];

	public static function operate($key=null){
		if(!is_null($key)){
			return self::$operate[$key];
		}else{
			return self::$operate;
		}
	}

	public static function flag($key=null){
		if(!is_null($key)){
			return self::$flag[$key];
		}else{
			return self::$flag;
		}
	}

	protected function setCreateTimeAttr(){
		return time();
	}

	/**
     * 获取分页列表
     * @param  [type] $where    [description]
     * @param  string $queryStr [description]
     * @param  string $baseUrl  [description]
     * @return [type]           [description]
     */
    public function getPageDatas($where,$page,$perpagenum){
        
        $start = ($page-1)*$perpagenum;
        
        $whereData=[];
        if(!empty($where)){
            foreach($where as $k => $wh):
                $whereData['a.'.$k] = $wh;
            endforeach;
        }
        
        $rows = $this
                ->alias('a')
                ->join('member b','a.uid=b.id')
                ->where($whereData)
                ->field('a.*,b.mid mid')
                ->limit($start,$perpagenum)
                ->order('a.id','DESC')->select();

        $count = $this->alias('a')->where($whereData)->count();
        $data = [];
        if($count){
            foreach($rows as $row):
                $row = $row->toArray();
                $data[] = $this->setField($row);
            endforeach;
        }

        $total_page = ceil($count/$perpagenum);
        return [
            'total_page' => $total_page,
            'curent_page' => $page,
            'count' => $count,
            'data' => $data
        ];
    }

    /**
     * 处理字段数据
     * @param [type] $row [description]
     */
    public function setField($row){
        if(isset($row['operate'])){
            $row['operate_remark'] = self::$operate[$row['operate']];
        }
        if(isset($row['flag'])){
            $row['flag_remark'] = self::flag($row['flag']);
        }
        if(isset($row['coin_id'])){
            $row['coin_name'] = Coin::coinData($row['coin_id'])['coin_name'];
        }
        if(isset($row['create_time'])){
            $row['create_date'] = date("Y-m-d H:i",$row['create_time']);
        }
        return $row;
    }

}
